package com.boot.management_boot.service.imp;

import com.boot.management_boot.Utils.time_mouth;
import com.boot.management_boot.mapper.BootMapper;
import com.boot.management_boot.pojo.Admin_allinfo;
import com.boot.management_boot.pojo.User;
import com.boot.management_boot.pojo.User_info;
import com.boot.management_boot.service.BootService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.logging.SimpleFormatter;

@Service
public class BootServiceImpl implements BootService {
    //依赖注入boot
    @Autowired
    BootMapper mapp;


    @Override
    public User login(String username, String password) {
        return mapp.login(username,password);
    }

    @Override
    public List<User_info> queryuserInfo(int uid) {
        return mapp.queryuserInfo(uid);
    }

    @Override
    public int update_user_password(String username, String password) {
        return mapp.update_user_password(username, password);
    }

    @Override
    public int query_user_name(String username) {
        return mapp.query_user_name(username);
    }

    @Override
    public int add_user(String username,String password,String edu) {
        return mapp.add_user(username, password, edu);
    }

//    @Override
//    public List<Map> query_all_info_user() {
//        return mapp.query_all_info_user();
//    }

    @Override
    public List<User_info> queryuserInfo_m(int uid) {
        Calendar cal = Calendar.getInstance();
        int i = cal.get(Calendar.MONTH) + 1;
        String firstDayOfMonth = time_mouth.getFirstDayOfMonth(i);
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH)+1;
//        int day = cal.get(Calendar.DATE) -1;
        int day1 = cal.get(Calendar.DATE);
        String lastDayOfMonth = year+"-"+month+"-"+day1;
        return mapp.queryuserInfo_m(uid,firstDayOfMonth,lastDayOfMonth);
    }

    @Override
    public List<User_info> queryuserInfo_get_time(int uid, String time) throws ParseException {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date parse = dateFormat.parse(time);
        String firstDayOfMonth = dateFormat.format(parse);//获取到的时间转换为相应的格式
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH)+1;
//        int day = cal.get(Calendar.DATE) -1;
        int day1 = cal.get(Calendar.DATE);
        String lastDayOfMonth = year+"-"+month+"-"+day1;//拼接当前日期前一天日期
        return mapp.queryuserInfo_m(uid,firstDayOfMonth,lastDayOfMonth);
    }

    @Override
    public List<User_info> queryuserInfo_get_time1(int uid, String time,String time2) throws ParseException {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date parse = dateFormat.parse(time);
        String firstDayOfMonth = dateFormat.format(parse);//获取到的时间转换为相应的格式
        Date parse1 = dateFormat.parse(time2);
        String lastDayOfMonth = dateFormat.format(parse1);
     /*   Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH)+1;
//        int day = cal.get(Calendar.DATE) -1;
        int day1 = cal.get(Calendar.DATE);
        String lastDayOfMonth = year+"-"+month+"-"+day1;//拼接当前日期前一天日期*/
        return mapp.queryuserInfo_m(uid,firstDayOfMonth,lastDayOfMonth);
    }
    @Override
    public List<Map> admin_queryuserallinfo() {
        return mapp.admin_queryuserallinfo();
    }

    @Override
    public int admin_updatepassword(String username, String password, int id) {
        return mapp.admin_updatepassword(username, password, id);
    }

    @Override
    public int admin_deluser(int id) {
        return mapp.admin_deluser(id);
    }

    @Override
    public void admin_info_f_t(int id, String info_time, int info_liushui, int info_shouyi,int column) {
        User_info user_info = mapp.query_info_f_t(id, info_time);
        if(user_info==null){
            if(column==1){
                int i = mapp.insert_info_liushui(id, info_time, info_liushui);
            }else if(column==2){
                int i = mapp.insert_info_shouyi(id, info_time, info_shouyi);
            }
        }else{
            if(column==1){
                int i = mapp.update_info_liushui(id, info_time, info_liushui);
            }else if(column==2){
                int i = mapp.update_info_shouyi(id, info_time, info_shouyi);
            }
        }
    }
    //删除用户并且删除此用户所有的数据
    @Override
    public int del_user(int id) {
        int i = mapp.del_user(id);
        int i1 = mapp.del_userinfo(id);
        return i;
    }


    @Override
    public int del_userinfo_time(int user_id, String time, String time1) throws ParseException {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date parse = dateFormat.parse(time);
        String firstDayOfMonth = dateFormat.format(parse);//获取到的时间转换为相应的格式
        Date parse1 = dateFormat.parse(time1);
        String lastDayOfMonth = dateFormat.format(parse1);
        return mapp.del_userinfo_time(user_id,firstDayOfMonth,lastDayOfMonth);
    }

    @Override
    public int admin_updateedu(String edu, String username, int id) {
        return mapp.admin_updateedu(edu, username, id);
    }

    @Override
    public int add_admin_user(String username, String password) {
        return mapp.add_admin_user(username,password);
    }

    @Override
    public List<Map> super_admin_user() {
        return mapp.super_admin_user();
    }

    @Override
    public Map query_user(String username) {
        return mapp.query_user(username);
    }


}
